package com.flying.invest.assets.service;

import java.util.Date;

import com.flying.common.annotation.DaoCreate;
import com.flying.common.annotation.DaoQuery;
import com.flying.common.annotation.DaoRemove;
import com.flying.common.annotation.DaoUpdate;
import com.flying.common.util.Codes;
import com.flying.common.util.DateUtils;
import com.flying.framework.annotation.MethodInfo;
import com.flying.framework.annotation.Param;
import com.flying.framework.annotation.Service;
import com.flying.framework.model.ActionResult;
import com.flying.framework.model.ModelFactory;
import com.flying.framework.model.QueryResult;
import com.flying.framework.service.AbstractService;
import com.flying.invest.assets.model.AssetsPrice;

@Service(value="AssetsPriceService", desc="")
public class AssetsPriceService extends AbstractService{
	@MethodInfo("新增")
	@DaoCreate(entity=AssetsPrice.ENTITY_NAME)
	public AssetsPrice create(
			@Param(value=AssetsPrice.ASSETS_ID, required=true,	desc="品种ID") Long assets_id,
			@Param(value=AssetsPrice.PRICE, required=false,	desc="价格") Double price,
			@Param(value=AssetsPrice.MIN_PRICE, required=false,	desc="最低价格") Double min_price,
			@Param(value=AssetsPrice.MAX_PRICE, required=false,	desc="最高价格") Double max_price,
			@Param(value=AssetsPrice.OPEN_PRICE, required=false,	desc="开盘价格") Double open_price,
			@Param(value=AssetsPrice.CLOSE_PRICE, required=false,	desc="收盘价格") Double close_price,
			@Param(value=AssetsPrice.PRICE_DATE, required=true,	desc="价格日期") String price_date,
			@Param(value=AssetsPrice.UPDATE_TIME, required=false,	desc="更新时间") Date update_time
		
		) {
		if(price == null) price = close_price;
		return ModelFactory.create(AssetsPrice.class, 
				AssetsPrice.PRICE, price, 
				AssetsPrice.UPDATE_TIME, new Date());
	}

	@MethodInfo("修改")
	@DaoUpdate(entity=AssetsPrice.ENTITY_NAME)
	public ActionResult update(
			@Param(value=AssetsPrice.PRICE_ID, required=true,	desc="ID") Long price_id,
			@Param(value=AssetsPrice.PRICE, required=false,	desc="价格") Double price,
			@Param(value=AssetsPrice.MIN_PRICE, required=false,	desc="最低价格") Double min_price,
			@Param(value=AssetsPrice.MAX_PRICE, required=false,	desc="最高价格") Double max_price,
			@Param(value=AssetsPrice.OPEN_PRICE, required=false,	desc="开盘价格") Double open_price,
			@Param(value=AssetsPrice.CLOSE_PRICE, required=false,	desc="收盘价格") Double close_price,
			@Param(value=AssetsPrice.UPDATE_TIME, required=false,	desc="更新时间") Date update_time
			
		) {
		if(price == null) price = close_price;
		return ModelFactory.create(ActionResult.class, 
				AssetsPrice.PRICE, price, 
				AssetsPrice.UPDATE_TIME, new Date());
	}

	@MethodInfo("修改")
	protected ActionResult updatePrice(

			@Param(value=AssetsPrice.ASSETS_ID, required=true,	desc="品种ID") Long assets_id,
			@Param(value=AssetsPrice.PRICE_DATE, required=true,	desc="价格日期") String price_date,
			@Param(value=AssetsPrice.PRICE, required=true,	desc="价格") Double price
			
		) {
		AssetsPrice p = null;
		try {
			p = this.findByAssetsId(assets_id, price_date);
			p.setPrice(price);
			if(p.getMinPrice() == null || p.getMinPrice() != null && p.getMinPrice() > price) {
				p.setMinPrice(price);
			}
			if(p.getMaxPrice() == null || p.getMaxPrice() != null && p.getMaxPrice() < price) {
				p.setMaxPrice(price);
			}
			if(p.getOpenPrice() == null) {
				p.setOpenPrice(price);
			}
			p.setClosePrice(price);
			this.update(p.getPriceId(), price, p.getMinPrice(), p.getMaxPrice(), p.getOpenPrice(), p.getClosePrice(), null);
		} catch (Exception e) {
			p = this.create(assets_id, price, price, price, price, price, price_date, new Date());
		}
		return ModelFactory.create(ActionResult.class, 
				AssetsPrice.UPDATE_TIME, new Date());
	}

	@MethodInfo("删除")
	@DaoRemove(entity=AssetsPrice.ENTITY_NAME)
	public ActionResult remove(
			@Param(value=AssetsPrice.PRICE_ID, required=true,	desc="ID") Long price_id) {
		return null;
	}

	@MethodInfo("删除")
	@DaoRemove(entity=AssetsPrice.ENTITY_NAME)
	public ActionResult removeByDate(
			@Param(value=AssetsPrice.ASSETS_ID, required=true,	desc="品种ID") Long assets_id,
			@Param(value=AssetsPrice.PRICE_DATE, required=true,	desc="价格日期") String price_date) {
		return null;
	}

	@MethodInfo("删除")
	@DaoRemove(entity=AssetsPrice.ENTITY_NAME)
	public ActionResult removeByAssetsId(
			@Param(value=AssetsPrice.ASSETS_ID, required=true,	desc="品种ID") Long assets_id) {
		return null;
	}

	@MethodInfo("搜索")
	@DaoQuery(entity=AssetsPrice.ENTITY_NAME, pageable=true, modelClass=AssetsPrice.class, osql="order by price_date desc")
	public QueryResult<AssetsPrice> findAll(
			@Param(value=AssetsPrice.ASSETS_ID, required=true,	desc="品种ID") Long assets_id,
			@Param(value=Codes.PAGE_NUM, 	required=false, desc="分页页号，默认0") int page,
			@Param(value=Codes.PAGE_SIZE, 	required=false, desc="分页大小，默认10") int rows) {
		return null;
	}

	@MethodInfo("搜索")
	@DaoQuery(entity=AssetsPrice.ENTITY_NAME, pageable=true, modelClass=AssetsPrice.class, osql="order by price_date")
	public QueryResult<AssetsPrice> findAllASC(
			@Param(value=AssetsPrice.ASSETS_ID, required=true,	desc="品种ID") Long assets_id,
			@Param(value=AssetsPrice.PRICE_DATE, required=true,	desc="价格日期", tag="BETWEEN") String[] price_dates,
			@Param(value=Codes.PAGE_NUM, 	required=false, desc="分页页号，默认0") int page,
			@Param(value=Codes.PAGE_SIZE, 	required=false, desc="分页大小，默认10") int rows) {
		return null;
	}

	@MethodInfo("搜索")
	@DaoQuery(entity=AssetsPrice.ENTITY_NAME, pageable=true, modelClass=AssetsPrice.class, osql="order by price_date desc")
	public QueryResult<AssetsPrice> findAllDESC(
			@Param(value=AssetsPrice.ASSETS_ID, required=true,	desc="品种ID") Long assets_id,
			@Param(value=AssetsPrice.PRICE_DATE, required=true,	desc="价格日期", tag="BETWEEN") String[] price_dates,
			@Param(value=Codes.PAGE_NUM, 	required=false, desc="分页页号，默认0") int page,
			@Param(value=Codes.PAGE_SIZE, 	required=false, desc="分页大小，默认10") int rows) {
		return null;
	}

	@MethodInfo("查询")
	@DaoQuery(entity=AssetsPrice.ENTITY_NAME, single=true, throwsNotFoundException=true )
	public AssetsPrice findByAssetsId(
			@Param(value=AssetsPrice.ASSETS_ID, required=true,	desc="品种ID") Long assets_id,
			@Param(value=AssetsPrice.PRICE_DATE, required=true,	desc="价格日期") String price_date) {
		return null;
	}

	//----------------
	@MethodInfo("查询资产时间段内最高/最低价格")
	@DaoQuery(entity=AssetsPrice.ENTITY_NAME, single=true, throwsNotFoundException=true )
	protected AssetsPrice findTopPriceByAssetsId2(
			@Param(value=AssetsPrice.ASSETS_ID, required=true,	desc="品种ID") Long assets_id,
			@Param(value=AssetsPrice.PRICE_DATE, required=true,	desc="价格日期", tag="BETWEEN") String[] price_date) {
		return null;
	}

	@MethodInfo("查询")
	public AssetsPrice findTopPriceByAssetsId(
			@Param(value=AssetsPrice.ASSETS_ID, required=true,	desc="品种ID") Long assets_id,
			@Param(value="from_date", required=true,	desc="开始日期") Date from_date,
			@Param(value="to_date", required=true,	desc="截止日期") Date to_date) {
		String[] dates = new String[] {DateUtils.formatDate(from_date, "yyyy-MM-dd"), DateUtils.formatDate(to_date, "yyyy-MM-dd")};
		AssetsPrice result = this.findTopPriceByAssetsId2(assets_id, dates);
		result.setAssetsId(assets_id);
		return result;
	}
	//
	@MethodInfo("查询资产时间段内最高/最低价格")
	@DaoQuery(entity=AssetsPrice.ENTITY_NAME, pageable=false, modelClass=AssetsPrice.class, 
		wsql="not exists(select 1 from i_holiday h, i_assets a where a.currency=h.currency and t.assets_id =a.assets_id and t.price_date=h.holiday) ", 
		osql="order by price_date desc")
	protected QueryResult<AssetsPrice> findPriceByAssetsId2(
			@Param(value=AssetsPrice.ASSETS_ID, required=true,	desc="品种ID") Long assets_id,
			@Param(value=AssetsPrice.PRICE_DATE, required=true,	desc="价格日期", tag="BETWEEN") String[] price_date
		) {
		return null;
	}

	@MethodInfo("查询")
	public QueryResult<AssetsPrice> findPriceByAssetsId(
			@Param(value=AssetsPrice.ASSETS_ID, required=true,	desc="品种ID") Long assets_id,
			@Param(value="from_date", required=true,	desc="开始日期") Date from_date,
			@Param(value="to_date", required=false,	desc="截止日期") Date to_date
		) {
		if(to_date == null) to_date = new Date();
		String[] dates = new String[] {DateUtils.formatDate(from_date, "yyyy-MM-dd"), DateUtils.formatDate(to_date, "yyyy-MM-dd")};
		return this.findPriceByAssetsId2(assets_id, dates);
	}
}